import os
import json
class Admin():
    #管理员账户密码设置
    def admin_set(self):
        # 如果self没有AdminUserName属性或者没有AdminUserPassword属性，那么整个条件将为True，
        # 接下来的代码块（在这段代码中并没有给出）将会被执行。这通常用于初始化或重置这些属性的情况。
        if not hasattr(self, 'AdminUserName') or not hasattr(self, 'AdminUserPassword'):
            self.AdminUserName = input("请重置管理员用户名")
            self.AdminUserPassword = input("请重置管理员密码")

        # 使用with语句确保文件正确关闭
        with open('AdminUsernameAndpassword.txt', 'w',encoding='utf-8') as file:
            # 存储用户名和密码的字典
            data = {'username': self.AdminUserName, 'password': self.AdminUserPassword}
            # 将字典转化为字符串并写入文件，这里假设你想以JSON格式存储数据
            file.write(json.dumps(data))

    #展示当前管理员
    def show_admin(self):
        with open('AdminUsernameAndpassword.txt', 'r') as file:
            lines = file.readlines()
            #print("当前查询到管理员人数为{}".format(len(lines)))
            for line in lines:
                #字符串转字典然后取值
                account=json.loads(line.strip())["username"]
                password=json.loads(line.strip())["password"]
                return account,password

    #增加学生
    def add_student(self):
        self.StudentUserName = input("请输入学生用户名")
        self.StudentUserPassword = input("请输入学生密码")
        with open('StudentUsernameAndpassword.txt', 'a') as file:
            data = {
                'username': self.StudentUserName,
                'password': self.StudentUserPassword
            }
            file.write(json.dumps(data)+'\n')

    #删除学生
    def delete_student(self):
        username_to_remove =input("请输入要删除的学生")
        with open('StudentUsernameAndpassword.txt', 'r') as file:
            lines = file.readlines()
        filtered_lines = []
        for line in lines:
            data = json.loads(line)
            if data['username'] != username_to_remove:
                filtered_lines.append(line)
        with open('StudentUsernameAndpassword.txt', 'w') as file:
            file.writelines(filtered_lines)

    #查询学生列表
    def show_all_student(self):
        with open('StudentUsernameAndpassword.txt', 'r') as file:
            lines = file.readlines()
            print("当前查询到学生人数为{}".format(len(lines)))
            for line in lines:
                #字符串转字典然后取值
                d=json.loads(line.strip())["username"]
                print(d)

    #增加教师
    def add_teacher(self):
        # 获取教师用户名和密码
        self.TeacherUserName = input("请输入教师用户名")
        self.TeacherUserPassword = input("请输入教师密码")

        # 验证密码（可选）
        # 在这里添加密码验证逻辑，例如检查密码的长度和强度

        # 存储用户名和密码
        with open('TeacherUsernameAndpassword.txt', 'a') as file:
            data = {
                'username': self.TeacherUserName,
                'password': self.TeacherUserPassword  # 假设这里只是存储明文密码，不推荐在生产环境中这样做
            }
            file.write(json.dumps(data)+'\n')

    #删除教师
    def delete_teacher(self):
        # 读取文件内容
        username_to_remove =input("请输入要删除的教师")
        # 打开文件并逐行读取内容
        with open('TeacherUsernameAndpassword.txt', 'r') as file:
            lines = file.readlines()
            # 初始化一个空列表用于存储过滤后的行
        filtered_lines = []
        # 遍历每一行
        for line in lines:
            # 将行转换为字典
            data = json.loads(line)
            # 检查是否要删除该行
            if data['username'] != username_to_remove:
                filtered_lines.append(line)  # 如果不是，将其添加到过滤后的行列表中
        # 将过滤后的行写回文件
        with open('TeacherUsernameAndpassword.txt', 'w') as file:
            file.writelines(filtered_lines)

    #查询教师列表
    def show_all_teacher(self):
        with open('TeacherUsernameAndpassword.txt', 'r') as file:
            lines = file.readlines()
            print("当前查询到教师人数为{}".format(len(lines)))
            for line in lines:
                #字符串转字典然后取值
                d=json.loads(line.strip())["username"]
                print(d)


if __name__ == '__main__':
    pass
    #a=Admin()
    #a.admin_set()
    #a.show_admin()
    #a.add_teacher()
    #a.show_all_teacher()
    #a.delete_teacher()
    #a.add_student()
    #a.delete_student()
    #a.show_all_student()
# print("当前目录是:",os.getcwd())
# files = os.listdir(os.getcwd())
#
# # 打印当前目录下的文件
# for file in files:
#     print(file)
